{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b4002afc-975a-4f8b-a0a1-3301aa2d282d",
   "metadata": {},
   "source": [
    "使用Python代码结合Selemiu工具实现自动化登录与网页信息采集：以“电子商务数据采集”为例\n",
    "\n",
    "任务要求：\n",
    "\n",
    "使用Python代码与Selemiu库实现自动化登录百度首页。\n",
    "输入关键词“电子商务数据采集”并进行搜索。\n",
    "获取搜索结果第一页的网页内容并保存为HTML文件。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2d4aef53-20a9-47ab-b4f7-96db8bf4354a",
   "metadata": {},
   "outputs": [
    {
     "ename": "NoSuchElementException",
     "evalue": "Message: no such element: Unable to locate element: {\"method\":\"css selector\",\"selector\":\"#\\33 001 .ec_desc\"}\n  (Session info: chrome=130.0.6723.59); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception\nStacktrace:\n\tGetHandleVerifier [0x00007FF615493AB5+28005]\n\t(No symbol) [0x00007FF6153F83B0]\n\t(No symbol) [0x00007FF61529580A]\n\t(No symbol) [0x00007FF6152E5A3E]\n\t(No symbol) [0x00007FF6152E5D2C]\n\t(No symbol) [0x00007FF61532EA97]\n\t(No symbol) [0x00007FF61530BA7F]\n\t(No symbol) [0x00007FF61532B8B3]\n\t(No symbol) [0x00007FF61530B7E3]\n\t(No symbol) [0x00007FF6152D75C8]\n\t(No symbol) [0x00007FF6152D8731]\n\tGetHandleVerifier [0x00007FF61578643D+3118829]\n\tGetHandleVerifier [0x00007FF6157D6C90+3448640]\n\tGetHandleVerifier [0x00007FF6157CCF0D+3408317]\n\tGetHandleVerifier [0x00007FF61555A40B+841403]\n\t(No symbol) [0x00007FF61540340F]\n\t(No symbol) [0x00007FF6153FF484]\n\t(No symbol) [0x00007FF6153FF61D]\n\t(No symbol) [0x00007FF6153EEB79]\n\tBaseThreadInitThunk [0x00007FFA9BAB259D+29]\n\tRtlUserThreadStart [0x00007FFA9C5EAF38+40]\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNoSuchElementException\u001b[0m                    Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 39\u001b[0m\n\u001b[0;32m     36\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m2\u001b[39m)\n\u001b[0;32m     38\u001b[0m \u001b[38;5;66;03m# 悬停在指定的元素上\u001b[39;00m\n\u001b[1;32m---> 39\u001b[0m element \u001b[38;5;241m=\u001b[39m driver\u001b[38;5;241m.\u001b[39mfind_element(By\u001b[38;5;241m.\u001b[39mCSS_SELECTOR, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m#\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;124m33 001 .ec_desc\u001b[39m\u001b[38;5;124m\"\u001b[39m)  \u001b[38;5;66;03m# 根据CSS选择器定位元素\u001b[39;00m\n\u001b[0;32m     40\u001b[0m actions \u001b[38;5;241m=\u001b[39m ActionChains(driver)  \u001b[38;5;66;03m# 创建动作链对象\u001b[39;00m\n\u001b[0;32m     41\u001b[0m actions\u001b[38;5;241m.\u001b[39mmove_to_element(element)\u001b[38;5;241m.\u001b[39mperform()  \u001b[38;5;66;03m# 悬停到目标元素上\u001b[39;00m\n",
      "File \u001b[1;32mD:\\anaconda3\\Lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py:748\u001b[0m, in \u001b[0;36mWebDriver.find_element\u001b[1;34m(self, by, value)\u001b[0m\n\u001b[0;32m    745\u001b[0m     by \u001b[38;5;241m=\u001b[39m By\u001b[38;5;241m.\u001b[39mCSS_SELECTOR\n\u001b[0;32m    746\u001b[0m     value \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m[name=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mvalue\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m]\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m--> 748\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mexecute(Command\u001b[38;5;241m.\u001b[39mFIND_ELEMENT, {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124musing\u001b[39m\u001b[38;5;124m\"\u001b[39m: by, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m: value})[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n",
      "File \u001b[1;32mD:\\anaconda3\\Lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py:354\u001b[0m, in \u001b[0;36mWebDriver.execute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    352\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcommand_executor\u001b[38;5;241m.\u001b[39mexecute(driver_command, params)\n\u001b[0;32m    353\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m response:\n\u001b[1;32m--> 354\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39merror_handler\u001b[38;5;241m.\u001b[39mcheck_response(response)\n\u001b[0;32m    355\u001b[0m     response[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_unwrap_value(response\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m))\n\u001b[0;32m    356\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m response\n",
      "File \u001b[1;32mD:\\anaconda3\\Lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py:229\u001b[0m, in \u001b[0;36mErrorHandler.check_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    227\u001b[0m         alert_text \u001b[38;5;241m=\u001b[39m value[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124malert\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtext\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    228\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m exception_class(message, screen, stacktrace, alert_text)  \u001b[38;5;66;03m# type: ignore[call-arg]  # mypy is not smart enough here\u001b[39;00m\n\u001b[1;32m--> 229\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exception_class(message, screen, stacktrace)\n",
      "\u001b[1;31mNoSuchElementException\u001b[0m: Message: no such element: Unable to locate element: {\"method\":\"css selector\",\"selector\":\"#\\33 001 .ec_desc\"}\n  (Session info: chrome=130.0.6723.59); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception\nStacktrace:\n\tGetHandleVerifier [0x00007FF615493AB5+28005]\n\t(No symbol) [0x00007FF6153F83B0]\n\t(No symbol) [0x00007FF61529580A]\n\t(No symbol) [0x00007FF6152E5A3E]\n\t(No symbol) [0x00007FF6152E5D2C]\n\t(No symbol) [0x00007FF61532EA97]\n\t(No symbol) [0x00007FF61530BA7F]\n\t(No symbol) [0x00007FF61532B8B3]\n\t(No symbol) [0x00007FF61530B7E3]\n\t(No symbol) [0x00007FF6152D75C8]\n\t(No symbol) [0x00007FF6152D8731]\n\tGetHandleVerifier [0x00007FF61578643D+3118829]\n\tGetHandleVerifier [0x00007FF6157D6C90+3448640]\n\tGetHandleVerifier [0x00007FF6157CCF0D+3408317]\n\tGetHandleVerifier [0x00007FF61555A40B+841403]\n\t(No symbol) [0x00007FF61540340F]\n\t(No symbol) [0x00007FF6153FF484]\n\t(No symbol) [0x00007FF6153FF61D]\n\t(No symbol) [0x00007FF6153EEB79]\n\tBaseThreadInitThunk [0x00007FFA9BAB259D+29]\n\tRtlUserThreadStart [0x00007FFA9C5EAF38+40]\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.common.action_chains import ActionChains\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "\n",
    "# 初始化Chrome浏览器驱动\n",
    "driver = webdriver.Chrome()\n",
    "driver.implicitly_wait(10)  # 隐式等待10秒，确保页面元素加载完成\n",
    "\n",
    "# 定义一个等待新窗口打开的函数\n",
    "def wait_for_window(driver, timeout=2):\n",
    "    # 等待指定的时间\n",
    "    time.sleep(timeout)\n",
    "    # 获取当前窗口句柄\n",
    "    wh_now = driver.window_handles\n",
    "    # 获取之前存储的窗口句柄\n",
    "    wh_then = vars.get(\"window_handles\", [])\n",
    "    # 如果有新的窗口打开，返回新窗口的句柄\n",
    "    if len(wh_now) > len(wh_then):\n",
    "        return list(set(wh_now) - set(wh_then))[0]\n",
    "    return None\n",
    "\n",
    "try:\n",
    "    # 打开百度首页\n",
    "    driver.get(\"https://www.baidu.com/\")\n",
    "    driver.set_window_size(1254, 800)  # 设置浏览器窗口大小\n",
    "\n",
    "    # 定位搜索框并输入搜索词\n",
    "    search_box = driver.find_element(By.ID, \"kw\")  # 根据ID定位搜索框\n",
    "    search_box.click()  # 点击搜索框\n",
    "    search_box.send_keys(\"电子商务聚集\")  # 输入搜索内容\n",
    "    search_box.send_keys(Keys.ENTER)  # 按下回车键以开始搜索\n",
    "\n",
    "    # 等待搜索结果加载\n",
    "    time.sleep(2)\n",
    "\n",
    "    # 悬停在指定的元素上\n",
    "    element = driver.find_element(By.CSS_SELECTOR, \"#\\\\33 001 .ec_desc\")  # 根据CSS选择器定位元素\n",
    "    actions = ActionChains(driver)  # 创建动作链对象\n",
    "    actions.move_to_element(element).perform()  # 悬停到目标元素上\n",
    "\n",
    "    # 存储当前窗口句柄\n",
    "    vars = {}\n",
    "    vars[\"window_handles\"] = driver.window_handles\n",
    "\n",
    "    # 点击某个搜索结果链接\n",
    "    driver.find_element(By.XPATH, \"//div[@id='1']/div/div/h3/a/em[2]\").click()  # 根据XPath定位链接并点击\n",
    "\n",
    "    # 等待新窗口打开并切换到新窗口\n",
    "    new_window = wait_for_window(driver, 2)  # 调用函数等待新窗口打开\n",
    "    if new_window:\n",
    "        driver.switch_to.window(new_window)  # 切换到新窗口\n",
    "\n",
    "    # 在新页面中与某个元素进行交互\n",
    "    driver.find_element(By.CSS_SELECTOR, \".base-wrap\").click()  # 根据CSS选择器定位元素并点击\n",
    "\n",
    "finally:\n",
    "    # 测试结束后退出浏览器\n",
    "    driver.quit()  # 关闭所有浏览器窗口并结束WebDriver会话\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84a060e7-edcc-4504-9181-62b5aea56801",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
